/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facebook;

import com.restfb.BinaryAttachment;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.Facebook;
import com.restfb.FacebookClient;
import com.restfb.FacebookClient.AccessToken;
import com.restfb.JsonMapper;
import com.restfb.Parameter;
import com.restfb.WebRequestor;
import com.restfb.batch.BatchRequest;
import com.restfb.batch.BatchResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
 *
 * @author Jose Luis
 */
public class FBRequest {

    private final String USER_AGENT = "Mozilla/5.0";

    public static void main(String[] args) throws Exception {

        FBRequest http = new FBRequest();

        System.out.println("Testing 1 - Send Http GET request");
        http.sendGet();
    }

    // HTTP GET request
    private void sendGet() throws Exception {

        String url = "https://graph.facebook.com/oauth/access_token?client_id=212044188860269&client_secret=213a73fb642f24b657b9bb98d0ad25f5&grant_type=client_credentials";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        //print result
        String access_token = response.toString();
        if (access_token != null && access_token != "") {
            StringTokenizer st = new StringTokenizer(access_token, "=");
            st.nextToken();
            access_token = st.nextToken();
        }
        System.out.println(access_token);

        /*FacebookClient facebookClientAppAccessToken = new DefaultFacebookClient(access_token);
        FacebookClient fb = new FacebookClient(access_token) {

            @Override
            public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T fetchObjects(List<String> ids, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> Connection<T> fetchConnectionPage(String connectionPageUrl, Class<T> connectionType) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> List<T> executeQuery(String query, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T executeMultiquery(Map<String, String> queries, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> List<T> executeFqlQuery(String query, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T executeFqlMultiquery(Map<String, String> queries, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public List<BatchResponse> executeBatch(BatchRequest... batchRequests) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T publish(String connection, Class<T> objectType, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public <T> T publish(String connection, Class<T> objectType, BinaryAttachment binaryAttachment, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public boolean deleteObject(String object, Parameter... parameters) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public AccessToken obtainAppAccessToken(String appId, String appSecret) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public AccessToken obtainExtendedAccessToken(String appId, String appSecret, String accessToken) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public JsonMapper getJsonMapper() {
                String s = new String("access_token");
                Facebook fb = new Facebook() {

                    @Override
                    public String value() {
                        return "access_token";
                    }

                    @Override
                    public Class<? extends Annotation> annotationType() {
                        Class c;
                    }
                };
            }

            @Override
            public WebRequestor getWebRequestor() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        };*/
        int p = 3;
    }
}
